﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Text;
using System.Xml;
using Axis.Web.Administrator.Views.Project;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Axis.BusinessObject.Core;
using System.Web.Script.Serialization;
using System.IO;

namespace Axis.Web.Views.Research
{
    public class ResearchUI : System.Web.UI.Page
    {
        CResearchDao cResearchDao = null;
        public ResearchUI()
        {
            cResearchDao = new CResearchDao();
        }
        public IList<CProject> GetProject(string inputValue)
        {
            IList<CProject> list = cResearchDao.GetProject(inputValue);
            return list;
        }
        public int GetProjectType(string inputValue)
        {
            IList<CProject> list = cResearchDao.GetProject(inputValue);
            if (list != null)
                if (list.Count > 0)
                    if (list[0].ProjectType != null)
                        return (int)list[0].ProjectType;
            return 1;
        }
        public string GetProjectList(int pPageIndex, string pRowsPerPage = "10")
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            var member = new CMemberList();
            if (Session["MEMBERINFO"] != null)
                member = Session["MEMBERINFO"] as CMemberList;
            root.SetAttribute("UserID", member.ID.ToString());
            root.SetAttribute("SecrectToken", member.SecrectToken);
            root.SetAttribute("Context", "AvailableProject");
            root.SetAttribute("Type", "1");
            root.SetAttribute("Action", "GET");
            root.SetAttribute("PageIndex", pPageIndex.ToString());
            root.SetAttribute("RowsPerPage", pRowsPerPage);
            CResearchDao core = new CResearchDao();
            CCore cCore = new CCore();
            string inputValue = root.OuterXml;
            string ret = GetProjectListHTML(inputValue);

            return ret;
        }
        public int GetTotalPage()
        {
            if (cResearchDao != null)
                return cResearchDao.LastOutputValue.TotalPages;
            else
                return 1;
        }
        public string GetProjectListHTML(string inputValue)
        {
            StringBuilder ret = new StringBuilder();
            IList<CProject> list = GetProject(inputValue);
            if(list!=null)
            for (int i = 0; i < list.Count; i++) list[i].ZOrder = i + 1;
            if (list != null && list.Count > 0)
            {
                foreach (CProject project in list)
                {
                    int index = list.IndexOf(project) + 1;
                    if (index == 3)
                    {
                        string temp =
                        "     <div class='research_item research_item_stamp content content_tag network' row-num='2.5'>                                          " +
                        "        <div class='content-tl'>                                                                                                      " +
                        "           <div class='content-tr'></div>                                                                                             " +
                        "       </div>                                                                                                                         " +
                        "       <div class='content-bl'>                                                                                                       " +
                        "            <div class='content-br'>                                                                                                  " +
                        "               <div class='content-main'>                                                                                             " +
                        "                   <div class='content-mask-tl'></div>                                                                                " +
                        "                   <div class='content-mask-tr'></div>                                                                                " +
                        "                   <div class='content-mask-bl'></div>                                                                                " +
                        "                   <div class='content-mask-br'></div>                                                                                " +
                        "                   <div class='content-inner'>                                                                                        " +
                        "                        <div class='content-top'>                                                                                     " +
                        "                           <div class='content-asset' style='visibility: hidden;'>                                                    " +
                        "                                <a href='/' class='ajax'></a>                                                                         " +
                        "                           </div>                                                                                                     " +
                        "                        </div>                                                                                                        " +
                        "                        <div class='content-info'  > <h2>Từ khóa</h2>                                                                   " +
                        "                           <div class='divider' style='padding-bottom: 4px;'><span></span></div>                                                                   " +
                        "                           <div class='tags project_tags_content tagCloud'></div>                                                         " +
                        "                        </div>                                                                                                        " +
                        "                   </div>                                                                                                             " +
                        "               </div>                                                                                                                 " +
                        "           </div>                                                                                                                     " +
                        "       </div>                                                                                                                         " +
                        "    </div>                                                                                                                            " +
                        "    <div class='research_item research_item_stamp content content_search network' row-num='2.6'>                                        " +
                        "        <div class='content-tl'>                                                                                                      " +
                        "            <div class='content-tr'></div>                                                                                            " +
                        "        </div>                                                                                                                        " +
                        "        <div class='content-bl'>                                                                                                      " +
                        "           <div class='content-br'>                                                                                                   " +
                        "               <div class='content-main'>                                                                                             " +
                        "                    <div class='content-mask-tl'></div>                                                                               " +
                        "                    <div class='content-mask-tr'></div>                                                                               " +
                        "                    <div class='content-mask-bl'></div>                                                                               " +
                        "                    <div class='content-mask-br'></div>                                                                               " +
                        "                    <div class='content-inner'>                                                                                       " +
                        "                        <div class='content-top'>                                                                                     " +
                        "                            <div class='content-asset' style='visibility: hidden;'>                                                   " +
                        "                                <a href='/' class='ajax'></a>                                                                         " +
                        "                            </div>                                                                                                    " +
                        "                        </div>                                                                                                        " +
                        "                        <div class='content-info' style='padding-top: 5px;'>                                                          " +
                        "                            <div><h2 style='padding: 0; font-size: 12px; float: left; line-height: 27px; padding-right: 3px;'>Từ khóa</h2> " +
                        "                            <input type='text' style='width: 164px;' /></div>                                                               " +
                        "                            <div>                                                                                                     " +
                        "                                <button type='button' class='button' style='float: right;'>Tìm kiếm</button>                          " +
                        "                            </div>                                                                                                    " +
                        "                        </div>                                                                                                        " +
                        "                    </div>                                                                                                            " +
                        "                </div>                                                                                                                " +
                        "            </div>                                                                                                                    " +
                        "        </div>                                                                                                                        " +
                        "    </div>";

                        ret.Append(temp);
                    }
                    string html = GetProjectHTML(project);
                    html = html.Replace("[RowNumber]", index.ToString());
                    html = html.Replace("[Index]", index.ToString());
                    ret.Append(html);

                }
            }
            return ret.ToString();
        }
        private Random random = new Random();
        protected string GetProjectHTML(CProject project)
        {
            StringBuilder ret = new StringBuilder();
            if (project != null)
            {
                int id = project.ID;
                string name = project.Name;
                string summary = project.Summary;
                string desc = project.Description;
                string image1 = project.Image1;
                var html = GetProjectTemplate();
                string[] contentTypeList = new string[] { "network", "people", "work", "careers" };
                string contentType = contentTypeList[random.Next(0, contentTypeList.Length - 1)];
                html = html.Replace("[ContentType]", contentType);

                html = html.Replace("[zOrder]", project.ZOrder.ToString());

                html = html.Replace("[ID]", id.ToString());
                html = html.Replace("[Image]", image1);
                html = html.Replace("[Mark]", project.Mark.ToString());
                html = html.Replace("[EndDate]", project.EndDate.ToString("dd/MM/yyyy"));
                html = html.Replace("[Name]", name);
                html = html.Replace("[Summary]", summary);
                html = html.Replace("[Description]", desc);
                ret.Append(html);
            }
            return ret.ToString();
        }
        protected string GetProjectTemplate()
        {
            string ret = string.Empty;
            StreamReader streamReader = File.OpenText(HttpContext.Current.Server.MapPath("~/Views/template/Research_list.txt"));
            ret = streamReader.ReadLine();
            while (streamReader.ReadLine() != null)
            {
                ret += streamReader.ReadLine();
            }
            streamReader.Close();
            return ret;
        }
        public string GetChartData(string pProjectID)
        {
            CResearchDao researchDao = new CResearchDao();
            string inputValue = string.Format("<InputValue ProjectID='{0}' Context='ProjectVote' />", pProjectID);
            IList<CChartData> list = researchDao.GetChartData(inputValue);
            return new JavaScriptSerializer().Serialize(list);
        }
    }
}